import { createRouter, createWebHistory } from 'vue-router'
import { useUserStore } from '@/stores/'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {path:'/login',component:()=>import('../views/login/Login.vue')},
    {
      path:'/',
      component:() => import('../views/main/Layout.vue'),
      redirect:'/main/select',
      children:[
        {
          path:'/main/select',
          component:() => import('../views/main/Select.vue')
        },
        {
          path:'/main/mylessons',
          component:() => import('../views/main/MyLessons.vue')
        },
        {path:'/main/space',
          component:() => import('../views/main/Space.vue')
        }
      ]
    }],
})

router.beforeEach((to)=>{
  //如果没有token，且访问的是非登录页，拦截到登录，其他情况正常放行
  const userStore = useUserStore()
  if(!userStore.token && to.path !== '/login') return '/login'
})
export default router
